//
//  MYNewFeatureViewController.swift
//  MYWeiBo
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let itemCount = 4 //欢迎的页数

class MYNewFeatureViewController: UICollectionViewController {
    
    //collectionview 要一个流水布局
    private let layout = UICollectionViewFlowLayout()
    
    //在创建的时候，给collectionview赋值一个流水布局
    init(){
        super.init(collectionViewLayout:layout)
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareLayout()

    }
    
    //准备collectview的布局
    private func prepareLayout(){
        self.collectionView!.registerClass(MYNewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //设置流水布局
        //内容大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //取消弹簧
        collectionView?.bounces = false
        //取消水平滚动条
//        collectionView?.showsHorizontalScrollIndicator = false
        //分页
        collectionView?.pagingEnabled = true
  
    }

  }

extension MYNewFeatureViewController{
    /**
     *collectinview的代理方法
     */
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MYNewFeatureViewCell
        cell.index = indexPath.item
        return cell
    }
    
    /**
    *  scrollview 停止滚动时才会调用，在最后一页的时候，让按钮滚动
    */
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //求得当前的页数
        let index = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
        if index == itemCount - 1 {
            //取得当前cell
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! MYNewFeatureViewCell
            cell.startAnimation()
        }
    }
}











